'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview  联盟任务系统
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var unionMissionConf = require('../../../../config/unionMissionConf.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var roleService = require('../../../../services/roleService');
var constantUtil = require('../../../../util/constantUtil.js');

/**
 * 开始任务
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var task_id = msg.task_id;

    var union_task_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                ['role', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var unionTask_model = player_manager.unionTask;
                var union_task_num = unionTask_model.union_task_num;

                // 是否有进行的任务
                if (!!unionTask_model.start_task_id) {
                    return next(null, {
                        code: Code.UNION_NOT_QUEUE
                    });
                }

                // 是否还有任务次数
                if (!union_task_num) {
                    return next(null, {
                        code: Code.UNION_NOT_TASK_NUM
                    });
                }

                // 任务是否进行中
                if (!!unionTask_model.isTaskStart(task_id)) {
                    return next(null, {
                        code: Code.UNION_USER_START_UNIONTASK
                    });
                }

                // 任务是否为当前任务 如果是，标记时间开始
                if (!unionTask_model.isUnionTask(task_id)) {
                    return next(null, {
                        code: Code.UNION_USER_NOT_UNIONTASK
                    });
                }

                // 记录任务id
                unionTask_model.setQueue(task_id);

                // 记录任务开始次数
                unionTask_model.addTaskNum();
                union_task_list = unionTask_model.unionTask_list;
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    union_task_list: union_task_list
                }
            });
        }
    );
};
